<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row style="height:60px">
              <el-button type="primary" icon="el-icon-plus" size="small" @click="hAdd">新增角色</el-button>
            </el-row>

            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column
                type="index"
                label="序号"
                width="120"
              />
              <el-table-column
                prop="name"
                label="角色名称"
                width="240"
              />
              <el-table-column
                prop="description"
                label="描述"
              />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click.native="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click.native="hDel(row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页 -->
              <el-pagination
                :current-page="size"
                :page-sizes="[2, 4, 6, 8]"
                :page-size="page"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
        <!-- 对话框 -->
        <el-dialog
          :title="isEdit?'编辑角色':'新增角色'"
          :visible.sync="dialogFormVisible"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          @close="hClose"
        >
          <el-form
            ref="formRef"
            :model="form"
            label-width="100px"
            :rules="rules"
          >
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="form.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="form.description" />
            </el-form-item>
          </el-form>
          <el-row slot="footer" type="flex" justify="center">
            <el-col :span="6">
              <el-button size="small" @click="dialogFormVisible=false">取消</el-button>
              <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
            </el-col>
          </el-row>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { addRoles, delRolesById, editRolesById, getRoles } from '@/api/settings'
export default {
  data() {
    return {
      list: [],
      total: 0,
      page: 2, // 每页条数
      size: 1, // 页数
      dialogFormVisible: false,
      isEdit: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadgetRoles()
  },
  methods: {
    // 获取所有角色列表
    async loadgetRoles() {
      try {
        const { data } = await getRoles({ pagesize: this.page, page: this.size })
        console.log(data)
        this.list = data.data.rows
        this.total = data.data.total
      } catch (error) {
        console.log(error)
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page = val
      this.loadgetRoles()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.size = val
      this.loadgetRoles()
    },
    // 添加
    hAdd() {
      this.dialogFormVisible = true
      this.isEdit = false
    },
    async doAdd() {
      try {
        const { data } = await addRoles(this.form)
        console.log(data)
        this.dialogFormVisible = false
        this.total++
        const pageMax = Math.ceil(this.total / this.page)
        this.size = pageMax
        this.loadgetRoles()
      } catch (error) {
        console.log(error)
      }
    },
    // 修改
    hEdit(row) {
      this.dialogFormVisible = true
      this.isEdit = true
      this.form = { ...row }
    },
    async doEdit() {
      try {
        const { data } = await editRolesById(this.form)
        console.log(data)
        this.dialogFormVisible = false
        this.loadgetRoles()
      } catch (error) {
        console.log(error)
      }
    },
    // 提交
    hSubmit() {
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    // 删除
    hDel(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const { data } = await delRolesById(row.id)
        console.log(data)
        if (this.list.length === 1) {
          this.size--
        }
        if (this.size <= 0) {
          this.size = 1
        }
        if (data.code === 10000) {
          this.loadgetRoles()
        }
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 关闭弹窗
    hClose() {
      this.$refs.formRef.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    }
  }
}
</script>

<style>
</style>
